package controller

import (
	"IMChat/models"
	"IMChat/utils"
	"context"
	"log"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
)

// https://juejin.cn/post/7114563512898322463  操作详细解说
func Test(c *gin.Context) {
	a := c.Query("a")
	switch a {
	// http://localhost:8888/test?a=testUserAdd
	case "testUserAdd":
		testUserAdd(c)
		break
	// http://localhost:8888/test?a=testUserListone
	case "testUserListone":
		testUserListone(c)
		break
		// http://localhost:8888/test?a=testUserListmany1
	case "testUserListmany1":
		testUserListmany1(c)
		break
		// http://localhost:8888/test?a=testUserListmany2
	case "testUserListmany2":
		testUserListmany2(c)
		break
	// http://localhost:8888/test?a=testUserEdit
	case "testUserEdit":
		testUserEdit(c)
		break
	// http://localhost:8888/test?a=testUserDelete
	case "testUserDelete":
		testUserDelete(c)
		break
	default:
		c.HTML(http.StatusOK, "admin/login.html", gin.H{"title": "我是测试", "ce": "123456"})
	}
}

func testUserAdd(c *gin.Context) {
	ub := &models.Userbasic{
		Identity:  "43052319341221357",
		Account:   "a269074234",
		Password:  "123456",
		Nickname:  "新2",
		Sex:       1,
		Email:     "2695498@qq.com",
		Avatar:    "3434",
		CreatedAt: time.Now().Unix(),
		UpdatedAt: time.Now().Unix(),
	}
	models.Mongo.Collection(models.Userbasic{}.CollectionName()).InsertOne(context.Background(), ub)
	utils.OkWithData(c, "新增完成")
}

func testUserListone(c *gin.Context) {
	ub := new(models.Userbasic)
	err := models.Mongo.Collection(models.Userbasic{}.CollectionName()).
		FindOne(context.Background(), bson.D{{"account", "a269074234"}}).
		Decode(ub)

	if err != nil {
		log.Println("[DB ERROR]:%v\n", err)
		res2 := new(utils.Response)
		res2.Code = -1
		utils.FailWithMsg(c, res2.Code, err.Error())
		return
	}
	utils.OkWithData(c, "ub")
	return
}

type Userbasics struct {
	Userbasic []models.Userbasic
}

func testUserListmany1(c *gin.Context) {
	// 建立查询 （无查询条件）
	find, err := models.Mongo.Collection(models.Userbasic{}.CollectionName()).Find(context.Background(), bson.D{})
	if err != nil {
		log.Println(err)
		return
	}

	ps := []models.Userbasic{}
	find.All(context.Background(), &ps)
	utils.OkWithData(c, ps)
	return
}

func testUserListmany2(c *gin.Context) {
	// 建立查询 （无查询条件）
	find, err := models.Mongo.Collection(models.Userbasic{}.CollectionName()).Find(context.Background(), bson.D{})
	if err != nil {
		log.Println(err)
		return
	}
	// 遍历查询结果
	for find.Next(context.Background()) {
		var demo models.Userbasic
		// 解码绑定数据
		err = find.Decode(&demo)
		if err != nil {
			log.Println(err)
			return
		}
		log.Printf("%v", demo)
		utils.OkWithData(c, demo)
	}
	return
}

func testUserEdit(c *gin.Context) error {
	filter := bson.D{{"account", "a269074234"}}

	update := bson.D{
		{"$set", bson.D{
			{"password", "lomtom1"},
		}},
	}

	_, err := models.Mongo.Collection(models.Userbasic{}.CollectionName()).
		UpdateOne(context.Background(), filter, update)
	if err != nil {
		log.Printf("[DB ERROR]:%v\n", err)
		return err
	}
	utils.OkWithData(c, "编辑完成")
	return nil
}

func testUserDelete(c *gin.Context) error {
	_, err := models.Mongo.Collection(models.Userbasic{}.CollectionName()).
		DeleteOne(context.Background(), bson.M{"account": "a269074234"})
	if err != nil {
		log.Printf("[DB ERROR]:%v\n", err)
		return err
	}
	utils.OkWithData(c, "删除完成")
	return nil
}
